package com.uuzuche.lib_zxing.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uuzuche.lib_zxing.CommonUtils;
import com.uuzuche.lib_zxing.R;

/**
 * Initial the camera
 * <p>
 * 默认的二维码扫描Activity
 */
public class CaptureActivity extends AppCompatActivity {
    private ImageView btInputNumber, btFlashlight, ivQRCode, ivFlashlight;
    private boolean isLightEnable = false;
    private ImageView ivBack;

    private RelativeLayout rlInputNumber;
    private EditText etLockNumber;
    private Button btCommit;
    private TextView tvTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        CaptureFragment captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();
        captureFragment.setCameraInitCallBack(new CaptureFragment.CameraInitCallBack() {
            @Override
            public void callBack(Exception e) {
                if (e == null) {

                } else {
                    Log.e("TAG", "callBack: ", e);
                }
            }
        });
        btInputNumber = (ImageView) this.findViewById(R.id.bt_input_number);
        btFlashlight = (ImageView) this.findViewById(R.id.bt_flashlight);
        ivQRCode = (ImageView) this.findViewById(R.id.bt_input_number1);
        ivFlashlight = (ImageView) this.findViewById(R.id.bt_flashlight1);
        tvTitle = (TextView) findViewById(R.id.tv_title);


        ivBack = (ImageView) this.findViewById(R.id.iv_title_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //  CodeUtils.isLightEnable(isLightEnable);
        rlInputNumber = (RelativeLayout) this.findViewById(R.id.rl_input_bike_number);
        rlInputNumber.setVisibility(View.GONE);
        etLockNumber = (EditText) findViewById(R.id.et_input_bike_code);
        btCommit = (Button) findViewById(R.id.bt_commit_lock_number);

        btFlashlight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLightEnable = !isLightEnable;
                CodeUtils.isLightEnable(isLightEnable);
            }
        });
        tvTitle.setText("扫码骑行");
        btInputNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rlInputNumber.setVisibility(View.VISIBLE);
                tvTitle.setText("输入车编号");
            }
        });

        ivFlashlight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLightEnable = !isLightEnable;
                CodeUtils.isLightEnable(isLightEnable);
            }
        });
        ivQRCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rlInputNumber.setVisibility(View.GONE);
                tvTitle.setText("扫码骑行");
            }
        });

        btCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = etLockNumber.getText().toString().trim();
                if (CommonUtils.isEmpty(result)) {
                    Toast.makeText(CaptureActivity.this, getString(R.string.input_bike_number), Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent resultIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                bundle.putString(CodeUtils.RESULT_STRING, result);
                resultIntent.putExtras(bundle);
                CaptureActivity.this.setResult(RESULT_OK, resultIntent);
                CaptureActivity.this.finish();

            }
        });

    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            CaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CaptureActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            CaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CaptureActivity.this.finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isLightEnable = false;
        CodeUtils.isLightEnable(isLightEnable);
    }
}